<?php
Cinnamon::loadClass('UIComponent');

/**
 * Default User Interface Component
 *
 */
class Container extends UIComponent {
	/**
     * Components and Containers within this container 
	 */
	protected $children;
	
	/**
	 * Default constructor
	 */
	public function __construct($title=null) {
		parent::__construct();

		// initialize properties
		$this->title 	= $title;
		$this->renderer = '/view/container.php';
		
		$this->children = array();
	}
	
	/**
	 * Extends the default cloning behaviour by cloning the children of this component
	 */
	public function __clone() {
		foreach ( $this->children as $k=>$child ) {
			$child = clone $child;
			$child->setParent($this);
			$this->children[$k] = $child;
		}
	}
	
	/**
	 * Adds the specified component to this container
	 *
	 * @param object child the component to be added
	 */
	public function addChild($child) {
		array_push($this->children, $child);
		$child->setParent($this);
	}
	
	/**
	 * Adds the specified compoment to this container, at the specified position.
	 * 
	 * @param object child the compoment to be inserted
	 * @param integer index index at which the specified compoment is to be inserted
	 */
	public function addChildAt($child, $index) {
		array_insert_at($this->children, $index, $child);
		$child->setParent($this);
	}
	
	/**
	 * Returns the component at the specified position
	 *
	 * @param integer index index of internal component to be returned
	 * @return object the component at the specified position
	 */
	public function getChildAt($index) {
		return $this->children[$index];
	}
	
	/**
	 * Returns an array of components which are children of this container
	 *
	 * @return object the component at the specified position
	 */
	public function getAllChildren() {
		return $this->children;
	}
	
	public function getIndexOfChild($child) {
		return array_search($child, $this->children);
	}
	
	public function childrenCount() {
		return count($this->children);
	}
	
	/**
	 * Removes any components from this container
	 */
	public function removeAllChildren() {
		$this->children = array();
	}
	
	/**
	 * Removes the specified component from this container
	 *
	 * @param integer index the component to be removed
	 */
	public function removeChild($child) {
		$child->setParent(null);
		$this->children = array_value_remove($this->children, $child);
	}
	
	/**
	 * Removes the component at the specified position.
	 *
	 * @param integer index the position of the component to be removed
	 */
	public function removeChildAt($index) {
		$this->getChildAt($index)->setParent(null);
		$this->children = array_key_remove($this->children, $index);
	}
	
	/**
	 * Renders this container.
	 */
	public function render() {
		if ( Cinnamon::inMode('ajax') ) {
			$params		= Cinnamon::registry('params');
			$idlength 	= strlen($this->id);
			$complength = strlen($params->component);
			if ( $idlength <= $complength && strncmp($this->id, $params->component, $idlength) == 0 ) {
				foreach ( $this->getAllChildren() as $child ) {
					$idlength = strlen($child->id);
					if ( $idlength <= $complength && strncmp($child->id, $params->component, $idlength) == 0 ) {
						$child->render();
						return;
					}
				}
			}
			return;
		}
		
		parent::render();
	}
}
?>